package com.glsc.ngateway.platform.service.costallocation;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.platform.domain.costallocation.BusiSystemInfo;
import com.glsc.ngateway.platform.domain.costallocation.DepCostAllocation;
import com.glsc.ngateway.platform.domain.costallocation.IpDetailInfo;
import com.glsc.ngateway.platform.domain.costallocation.IpInfo;
import com.glsc.ngateway.platform.dto.BusiSystemInfoQueryDto;
import com.glsc.ngateway.platform.dto.BusiSystemStatisticDto;
import com.glsc.ngateway.platform.dto.DepartmentStaticDto;
import com.glsc.ngateway.platform.dto.IpInfoQueryDto;
import com.glsc.ngateway.platform.repo.costallocation.BusiSystemInfoRepo;
import com.glsc.ngateway.platform.repo.costallocation.IpDetailInfoRepo;
import com.glsc.ngateway.platform.repo.costallocation.IpInfoRepo;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.glsc.ngateway.common.api.common.enums.Constant.COSTALLOCATION_BUSISYSTEMINFO_ADMIN;
import static com.glsc.ngateway.common.api.common.enums.Constant.COSTALLOCATION_SYSTEMCOMPONENTDETAIL_ADMIN;

/**
 * @author guoj
 * @description: 统计
 * @date 2024/8/14 10:37
 */
@Service
public class CostallocationStatisticsService {
    private static Logger logger = LoggerFactory.getLogger(CostallocationStatisticsService.class);

    @Autowired
    private IpInfoService ipInfoService;
    @Resource
    private BusiSystemInfoRepo busiSystemInfoRepo;
    @Resource
    private DepCostAllocationService depCostAllocationService;
    @Autowired
    private IpInfoRepo ipInfoRepo;
    @Autowired
    private IpDetailInfoService ipDetailInfoService;


    public List<IpInfo> queryIpInfoList(IpInfoQueryDto ipInfoQueryDto){
        Map<String, Object> param = new HashMap<>();
        String ip = ipInfoQueryDto.getIp();
        if(StrUtil.isNotBlank(ip)){
            param.put("LIKE_ip",ip);
        }
        Map<String, Object> paramIpDetail = new HashMap<>();
        String systemClassNo = ipInfoQueryDto.getSystemClassNo();
        if (StrUtil.isNotEmpty(systemClassNo)) {
            paramIpDetail.put("EQ_systemClassNo", systemClassNo);
        }
        String componentName = ipInfoQueryDto.getComponentName();
        if (StrUtil.isNotEmpty(componentName)) {
            paramIpDetail.put("LIKE_componentName", componentName);
        }
        String componentCategory = ipInfoQueryDto.getComponentCategory();
        if (StrUtil.isNotEmpty(componentCategory)) {
            paramIpDetail.put("EQ_componentCategory", componentCategory);
        }
        List<IpInfo> content = ipInfoRepo.findAll(SpecificationUtil.buildSpecification(param, IpInfo.class));
        List<IpInfo> resultList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(content)){
            Set<Long> collect = content.stream().map(IpInfo::getId).collect(Collectors.toSet());
            Map<Long, List<IpDetailInfo>> systemid2List = ipDetailInfoService.queryByIpInfoIdSet(collect,paramIpDetail);
            for (IpInfo temp : content) {
                List<IpDetailInfo> ipDetailInfoList = systemid2List.get(temp.getId());
                if(CollectionUtil.isNotEmpty(ipDetailInfoList)){
                    temp.setIpDetailInfoList(ipDetailInfoList);
                    resultList.add(temp);
                }else{
                    if (paramIpDetail.size() == 0) {
                        temp.setIpDetailInfoList(new ArrayList<>());
                        resultList.add(temp);
                    }
                }
            }
        }
        return resultList;
    }


}
